package com.fmy.springbootsecurity.config;

import com.fmy.security.cache.config.CaffeineConfiguration;
import com.fmy.security.config.SecurityConfigAdapter;
import com.fmy.security.config.TokenProperties;
import com.fmy.security.service.login.CustUserDetailsService;
import com.github.benmanes.caffeine.cache.Ticker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName SecurityConfig.java
 * @Description TODO
 * @createTime 2019年07月06日 17:10:00
 */
@Configuration
public class SecurityConfig extends SecurityConfigAdapter {


    //使用Security方式去登录
//    @Autowired
//    private UserDetailsService userDetailsService;

    //使用扩展方式去登录
    @Bean
    @Primary
    public UserDetailsService userDetailsService() {
        return new CustUserDetailsService();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers("/login/**").permitAll()//login路径下的接口可以自由访问
                .antMatchers("/**").authenticated();
        http.formLogin().loginPage("/login/auth");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                return charSequence.equals(s);
            }
        });
    }

    /**
     * 自定义配置
     */
    @Override
    protected void configure(Config config) {
//        config.setMaxRegister(2);//设置同一用户并发量
//        config.getLoginTypeFilter().setKey("login_type")//设置登录时 获取登录方式的参数、默认login_type
//                .setRequestMatcher("/login/auth", HttpMethod.POST)//设置拦截的登录地址
//                .setTypes("app", "web", "we_caht");//设置默认的登录类型
////        默认不拦截路径 /login/** 默认拦截路径 /**
//        config.getTokenContextHolderFilter()
//                .notUrls("/login/**")//不拦截路径
//                .url("/**") //拦截路径 notUrls或url配置一个也可以、如果两个都配置那么之间的关系就是 and
//                .buildMatcher() //notUrls和url存在一个, 就需要调用该方法
//                .setTokenKey("Authorization");//获取token在请求头的key值
    }

    @Bean("couCacheManage") //在yml中配置一下即可
    public CacheManager couCacheManage(TokenProperties properties, Ticker ticker) {
        return new CaffeineConfiguration(properties).cacheManager(ticker);
    }

    @Bean("ehCacheManager")
    @Primary //指定默认缓存
    public CacheManager ehCacheManager() {
        return new EhCacheCacheManager();
    }
}
